#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "qminiqt.h"

#ifdef DEBUG
#define MAX_QDEBUG_TYPES 16

static const char *types[MAX_QDEBUG_TYPES];
static int types_no = 0;

void qDebugSetTypes(int no, ...)
{
	types_no = no;
	va_list ap;
	va_start(ap, no);
	for (int i = 0; i < no; ++i)
		types[i] = va_arg(ap, const char *);
	va_end(ap);
}

void qDebug(const char *type, const char *fmt, ...)
{
	if (type) {
		bool found = FALSE;
		for (int i = 0; i < types_no; ++i)
			if (!strcmp(type, types[i]))
				found = TRUE;
		if (!found)
			return;
	}

	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "[%s] ", type);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
}
#endif // DEBUG

void qWarning(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
}

void qFatal(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
	exit(1);
}
